package com.yun.util.yun.leetCode.simple;

public class BinaryTree {

    public static void main(String[] args) {

        int [] a = {1,3,2,5,7,6,4};
        int [] b = {1,2};
        int [] c = {8,9,10,5,7,6,4};
        System.out.println(judge(a,0,a.length-1));
    }

    /**
     *  递归判断二叉树
     * @param a               数组
     * @param leftBorder      数组左边界
     * @param rightBorder     数组右边界
     * @return                成功/失败
     */
    private static boolean judge(int[] a,int leftBorder,int rightBorder) {
        if (leftBorder==rightBorder){
            return true;
        }
        int rootNode = leftBorder;
        while(rootNode < rightBorder && a[rootNode] < a[rightBorder]){
            rootNode++;
        }
        for (int i = rootNode; i < rightBorder; i++) {
            if (a[i]<a[rightBorder]){
                return false;
            }
        }
        return judge(a,leftBorder,rootNode - 1)&&judge(a,rootNode,rightBorder - 1);
    }

}
